<template>
  <div class="wrapper">
      <el-form :inline="true">
        <el-form-item>
          <date-phase ref="datePhase" dateTypeRange="234" :datePhase="datePhase" ></date-phase>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="searchItem()">查询</el-button>
          <el-button type="primary" @click="exportExcel()">导出</el-button>
        </el-form-item>
      </el-form>

      <div>
        <el-table :height="tableHeight" ref="table" v-if="tableType === 1" :data="tableData"  border style="width: 100%;overflow-x: auto;" >
          <el-table-column label="时间" prop="date" min-width="180" align="center"></el-table-column>
          <el-table-column label="平台" prop="name" min-width="180" align="center"></el-table-column>
          <el-table-column label="花费($)" prop="cost" min-width="180" align="center">
            <template slot-scope="scope">
              {{scope.row.cost? scope.row.cost : '0'}}
            </template>
          </el-table-column>
          <el-table-column label="新增粉丝" prop="newFans" min-width="100" align="center">
            <template slot-scope="scope">
              {{scope.row.newFans? scope.row.newFans : '0'}}
            </template>
          </el-table-column>
          <el-table-column label="粉丝成本" prop="costOfFans" min-width="100" align="center">
            <template slot-scope="scope">
              {{scope.row.costOfFans? scope.row.costOfFans : '0'}}
            </template>
          </el-table-column>
          <el-table-column label="视频浏览量" prop="viewNum" min-width="100" align="center">
            <template slot-scope="scope">
              {{scope.row.viewNum? scope.row.viewNum : '0'}}
            </template>
          </el-table-column>
          <el-table-column label="转换成本" prop="cpf" min-width="100" align="center">
            <template slot-scope="scope">
              {{scope.row.cpf? scope.row.cpf : '0'}}
            </template>
          </el-table-column>
          <el-table-column label="发帖数" prop="articleNum" min-width="100" align="center">
            <template slot-scope="scope">
              {{scope.row.articleNum? scope.row.articleNum : '0'}}
            </template>
          </el-table-column>
        </el-table>

        <el-table :height="tableHeight" ref="table" v-if="tableType === 4" :data="tableData"  border style="width: 100%;overflow-x: auto;" >
          <el-table-column label="时间" prop="date" min-width="160" align="center"></el-table-column>
          <el-table-column label="平台" prop="name" min-width="90" align="center"></el-table-column>
          <el-table-column label="花费($)" prop="cost" min-width="100" align="center">
            <template slot-scope="scope">
              {{scope.row.cost? scope.row.cost : '0'}}
            </template>
          </el-table-column>
          <el-table-column label="新增粉丝" prop="newFans" min-width="100" align="center">
            <template slot-scope="scope">
              {{scope.row.newFans? scope.row.newFans : '0'}}
            </template>
          </el-table-column>
          <el-table-column label="粉丝成本" prop="costOfFans" min-width="100" align="center">
            <template slot-scope="scope">
              {{scope.row.costOfFans? scope.row.costOfFans : '0'}}
            </template>
          </el-table-column>
          <el-table-column label="新创作数" prop="articleNum" min-width="100" align="center">
            <template slot-scope="scope">
              {{scope.row.articleNum? scope.row.articleNum : '0'}}
            </template>
          </el-table-column>
          <el-table-column label="优质创作" prop="carClickNum" min-width="100" align="center">
            <template slot-scope="scope">
              {{scope.row.carClickNum? scope.row.carClickNum : '0'}}
            </template>
          </el-table-column>
          <el-table-column label="业绩" prop="saleValue" min-width="100" align="center">
            <template slot-scope="scope">
              {{scope.row.saleValue? scope.row.saleValue : '0'}}
            </template>
          </el-table-column>
          <el-table-column label="浏览量" prop="viewNum" min-width="100" align="center">
            <template slot-scope="scope">
              {{scope.row.viewNum? scope.row.viewNum : '0'}}
            </template>
          </el-table-column>
          <el-table-column label="评论数" prop="newComments" min-width="100" align="center">
            <template slot-scope="scope">
              {{scope.row.newComments? scope.row.newComments : '0'}}
            </template>
          </el-table-column>
          <el-table-column label="点赞数" prop="greetNum" min-width="100" align="center">
            <template slot-scope="scope">
              {{scope.row.greetNum? scope.row.greetNum : '0'}}
            </template>
          </el-table-column>
          <el-table-column label="对话数" prop="quoteNum" min-width="100" align="center">
            <template slot-scope="scope">
              {{scope.row.quoteNum? scope.row.quoteNum : '0'}}
            </template>
          </el-table-column>
<!--          <el-table-column label="转换成本" prop="cpf" min-width="100" align="center">
            <template slot-scope="scope">
              {{scope.row.cpf? scope.row.cpf : '0'}}
            </template>
          </el-table-column>-->
        </el-table>

        <el-table :height="tableHeight" ref="table" v-if="tableType === 2" :data="tableData"  border style="width: 100%;overflow-x: auto;" >
          <el-table-column label="时间" prop="date" min-width="180" align="center"></el-table-column>
          <el-table-column label="平台" prop="name" min-width="180" align="center"></el-table-column>
          <el-table-column label="花费($)" prop="cost" min-width="180" align="center">
            <template slot-scope="scope">
              {{scope.row.cost? scope.row.cost : '0'}}
            </template>
          </el-table-column>
          <el-table-column label="访客数" prop="visitorNum" min-width="100" align="center">
            <template slot-scope="scope">
              {{scope.row.visitorNum? scope.row.visitorNum : '0'}}
            </template>
          </el-table-column>
          <el-table-column label="展示数" prop="showNum" min-width="180" align="center">
            <template slot-scope="scope">
              {{scope.row.showNum? scope.row.showNum : '0'}}
            </template>
          </el-table-column>
          <el-table-column label="点击数" prop="clickNum" min-width="180" align="center">
            <template slot-scope="scope">
              {{scope.row.clickNum? scope.row.clickNum : '0'}}
            </template>
          </el-table-column>
          <el-table-column label="点击率" prop="clickRate" min-width="180" align="center">
            <template slot-scope="scope">
              {{scope.row.clickRate? scope.row.clickRate + '%' : '0%'}}
            </template>
          </el-table-column>
          <el-table-column label="询盘数" prop="quoteNum" min-width="180" align="center">
            <template slot-scope="scope">
              {{scope.row.quoteNum? scope.row.quoteNum : '0'}}
            </template>
          </el-table-column>
          <el-table-column label="转换成本" prop="cpf" min-width="100" align="center">
            <template slot-scope="scope">
              {{scope.row.cpf? scope.row.cpf : '0'}}
            </template>
          </el-table-column>
        </el-table>

        <el-table :height="tableHeight" ref="table" v-if="tableType === 5" :data="tableData"  border style="width: 100%;overflow-x: auto;" >
          <el-table-column label="时间" prop="date" min-width="160" align="center"></el-table-column>
          <el-table-column label="平台" prop="name" min-width="100" align="center"></el-table-column>
          <el-table-column label="花费($)" prop="cost" min-width="100" align="center">
            <template slot-scope="scope">
              {{scope.row.cost? scope.row.cost : '0'}}
            </template>
          </el-table-column>
          <el-table-column label="访客数" prop="visitorNum" min-width="100" align="center">
            <template slot-scope="scope">
              {{scope.row.visitorNum? scope.row.visitorNum : '0'}}
            </template>
          </el-table-column>
          <el-table-column label="展示数" prop="showNum" min-width="100" align="center">
            <template slot-scope="scope">
              {{scope.row.showNum? scope.row.showNum : '0'}}
            </template>
          </el-table-column>
          <el-table-column label="点击数" prop="clickNum" min-width="100" align="center">
            <template slot-scope="scope">
              {{scope.row.clickNum? scope.row.clickNum : '0'}}
            </template>
          </el-table-column>
          <el-table-column label="点击率" prop="clickRate" min-width="100" align="center">
            <template slot-scope="scope">
              {{scope.row.clickRate? scope.row.clickRate + '%' : '0%'}}
            </template>
          </el-table-column>
          <el-table-column label="询盘数" prop="quoteNum" min-width="100" align="center">
            <template slot-scope="scope">
              {{scope.row.quoteNum? scope.row.quoteNum : '0'}}
            </template>
          </el-table-column>
          <el-table-column label="咨询数" prop="queryNum" min-width="100" align="center">
            <template slot-scope="scope">
              {{scope.row.queryNum? scope.row.queryNum : '0'}}
            </template>
          </el-table-column>
          <el-table-column label="转换成本" prop="cpf" min-width="100" align="center">
            <template slot-scope="scope">
              {{scope.row.cpf? scope.row.cpf : '0'}}
            </template>
          </el-table-column>
        </el-table>

        <el-table :height="tableHeight" ref="table" v-if="tableType === 3" :data="tableData"  border style="width: 100%;overflow-x: auto;" >
          <el-table-column label="时间" prop="date" min-width="180" align="center"></el-table-column>
          <el-table-column label="平台" prop="name" min-width="100" align="center"></el-table-column>
          <el-table-column label="花费($)" prop="totalCost" min-width="100" align="center">
            <template slot-scope="scope">
              {{scope.row.totalCost? scope.row.totalCost : '0'}}
            </template>
          </el-table-column>
          <el-table-column label="店铺" align="center">
            <el-table-column label="访客数" prop="visitorNum" min-width="100" align="center">
              <template slot-scope="scope">
                {{scope.row.visitorNum? scope.row.visitorNum : '0'}}
              </template>
            </el-table-column>
            <el-table-column label="曝光数" prop="showNum" min-width="100" align="center">
              <template slot-scope="scope">
                {{scope.row.showNum? scope.row.showNum : '0'}}
              </template>
            </el-table-column>
            <el-table-column label="点击数" prop="clickNum" min-width="100" align="center">
              <template slot-scope="scope">
                {{scope.row.clickNum? scope.row.clickNum : '0'}}
              </template>
            </el-table-column>
            <el-table-column label="点击率" prop="clickRate" min-width="100" align="center">
              <template slot-scope="scope">
                {{scope.row.clickRate? scope.row.clickRate + '%' : '0%'}}
              </template>
            </el-table-column>
            <el-table-column label="询盘数" prop="quoteNum" min-width="100" align="center">
              <template slot-scope="scope">
                {{scope.row.quoteNum? scope.row.quoteNum : '0'}}
              </template>
            </el-table-column>
            <el-table-column label="咨询数" prop="queryNum" min-width="100" align="center">
              <template slot-scope="scope">
                {{scope.row.queryNum? scope.row.queryNum : '0'}}
              </template>
            </el-table-column>
            <el-table-column label="CPF" prop="cpf" min-width="100" align="center">
              <template slot-scope="scope">
                {{scope.row.cpf? scope.row.cpf : '0'}}
              </template>
            </el-table-column>
          </el-table-column>
          <el-table-column label="外贸直通车" align="center">
            <el-table-column label="曝光数" prop="carShowNum" min-width="100" align="center">
              <template slot-scope="scope">
                {{scope.row.carShowNum? scope.row.carShowNum : '0'}}
              </template>
            </el-table-column>
            <el-table-column label="点击数" prop="carClickNum" min-width="100" align="center">
              <template slot-scope="scope">
                {{scope.row.carClickNum? scope.row.carClickNum : '0'}}
              </template>
            </el-table-column>
            <el-table-column label="点击率" prop="carClickRate" min-width="100" align="center">
              <template slot-scope="scope">
                {{scope.row.carClickRate? scope.row.carClickRate + '%' : '0%'}}
              </template>
            </el-table-column>
            <el-table-column label="花费($)" prop="cost" min-width="100" align="center">
              <template slot-scope="scope">
                {{scope.row.cost? scope.row.cost : '0'}}
              </template>
            </el-table-column>
            <el-table-column label="CPC" prop="carCpc" min-width="100" align="center">
              <template slot-scope="scope">
                {{scope.row.carCpc? scope.row.carCpc : '0'}}
              </template>
            </el-table-column>
          </el-table-column>
        </el-table>

        <el-table :height="tableHeight" ref="table" v-if="tableType === 6" :data="tableData"  border style="width: 100%;overflow-x: auto;" >
          <el-table-column label="时间" prop="date" min-width="180" align="center"></el-table-column>
          <el-table-column label="平台" prop="name" min-width="100" align="center"></el-table-column>
          <el-table-column label="花费($)" prop="totalCost" min-width="100" align="center">
            <template slot-scope="scope">
              {{scope.row.totalCost? scope.row.totalCost : '0'}}
            </template>
          </el-table-column>
          <el-table-column label="店铺" align="center">
            <el-table-column label="访客数" prop="visitorNum" min-width="100" align="center">
              <template slot-scope="scope">
                {{scope.row.visitorNum? scope.row.visitorNum : '0'}}
              </template>
            </el-table-column>
            <el-table-column label="曝光数" prop="showNum" min-width="100" align="center">
              <template slot-scope="scope">
                {{scope.row.showNum? scope.row.showNum : '0'}}
              </template>
            </el-table-column>
            <el-table-column label="点击数" prop="clickNum" min-width="100" align="center">
              <template slot-scope="scope">
                {{scope.row.clickNum? scope.row.clickNum : '0'}}
              </template>
            </el-table-column>
            <el-table-column label="点击率" prop="clickRate" min-width="100" align="center">
              <template slot-scope="scope">
                {{scope.row.clickRate? scope.row.clickRate + '%' : '0%'}}
              </template>
            </el-table-column>
            <el-table-column label="询盘数" prop="quoteNum" min-width="100" align="center">
              <template slot-scope="scope">
                {{scope.row.quoteNum? scope.row.quoteNum : '0'}}
              </template>
            </el-table-column>
            <el-table-column label="咨询数" prop="queryNum" min-width="100" align="center">
              <template slot-scope="scope">
                {{scope.row.queryNum? scope.row.queryNum : '0'}}
              </template>
            </el-table-column>
            <el-table-column label="CPF" prop="cpf" min-width="100" align="center">
              <template slot-scope="scope">
                {{scope.row.cpf? scope.row.cpf : '0'}}
              </template>
            </el-table-column>
          </el-table-column>
          <el-table-column label="外贸直通车" align="center">
            <el-table-column label="曝光数" prop="carShowNum" min-width="100" align="center">
              <template slot-scope="scope">
                {{scope.row.carShowNum? scope.row.carShowNum : '0'}}
              </template>
            </el-table-column>
            <el-table-column label="点击数" prop="carClickNum" min-width="100" align="center">
              <template slot-scope="scope">
                {{scope.row.carClickNum? scope.row.carClickNum : '0'}}
              </template>
            </el-table-column>
            <el-table-column label="点击率" prop="carClickRate" min-width="100" align="center">
              <template slot-scope="scope">
                {{scope.row.carClickRate? scope.row.carClickRate + '%' : '0%'}}
              </template>
            </el-table-column>
            <el-table-column label="花费($)" prop="cost" min-width="100" align="center">
              <template slot-scope="scope">
                {{scope.row.cost? scope.row.cost : '0'}}
              </template>
            </el-table-column>
            <el-table-column label="CPF" prop="carCpc" min-width="100" align="center">
              <template slot-scope="scope">
                {{scope.row.carCpc? scope.row.carCpc : '0'}}
              </template>
            </el-table-column>
          </el-table-column>
        </el-table>

        <el-table :height="tableHeight" ref="table" v-if="tableType === 7" :data="tableData"  border style="width: 100%;overflow-x: auto;" >
          <el-table-column label="时间" prop="date" min-width="180" align="center"></el-table-column>
          <el-table-column label="平台" prop="name" min-width="180" align="center"></el-table-column>
          <el-table-column label="花费($)" prop="cost" min-width="180" align="center">
            <template slot-scope="scope">
              {{scope.row.cost? scope.row.cost : '0'}}
            </template>
          </el-table-column>
          <el-table-column label="新增粉丝" prop="newFans" min-width="180" align="center">
            <template slot-scope="scope">
              {{scope.row.newFans? scope.row.newFans : '0'}}
            </template>
          </el-table-column>
          <el-table-column label="粉丝成本" prop="costOfFans" min-width="180" align="center">
            <template slot-scope="scope">
              {{scope.row.costOfFans? scope.row.costOfFans : '0'}}
            </template>
          </el-table-column>
          <el-table-column label="视频浏览量" prop="viewNum" min-width="180" align="center">
            <template slot-scope="scope">
              {{scope.row.viewNum? scope.row.viewNum : '0'}}
            </template>
          </el-table-column>
          <el-table-column label="转换成本" prop="cpf" min-width="180" align="center">
            <template slot-scope="scope">
              {{scope.row.cpf? scope.row.cpf : '0'}}
            </template>
          </el-table-column>
        </el-table>
      </div>
  </div>
</template>

<script>
  import dingMixin from  '@/mixins/ding'
  import formUtil from '@/utils/formUtil'
  import DatePhase from  '@/components/sale/date-phase'
  import FileSaver from 'file-saver'
  import XLSX from 'xlsx'

  export default {
    data () {
      return {
        countryData: [],
        tableData: [],
        defaultProps: {
          children: 'children',
          label: 'name',
          isLeaf: 'leaf'
        },
        datePhase:{
          dateType: 2,
          value:'',
          to:''
        },
        platId: '',
        name: '',
        treeData: [],
        tableHeight: window.tableHeight,
        tableType: 1 //
      }
    },
    components: {
      DatePhase
    },
    activated() {
      this.tableHeight = document.documentElement['clientHeight'] - 220
      window.onresize = () => {
        this.tableHeight = document.documentElement['clientHeight'] - 220
      }
    },
    mixins: [dingMixin],
    async mounted(){
      let pid = this.$route.query.pid;
      this.platId = this.$route.query.platId;
      this.name = this.$route.query.name;
      if(pid === '0') {
        if(this.platId === '100') {
          this.tableType = 1;
        }else if(this.platId === '200'){
          this.tableType = 2;
        }else if(this.platId === '300'){
          this.tableType = 3;
        }
      }else if(pid === '100'){
        this.tableType = 4;
      }else if(pid === '200'){
        this.tableType = 5;
      }else if(pid === '300'){
        this.tableType = 6;
      }else {
        this.tableType = 7;
      }

      this.datePhase.to = '';
      this.datePhase.value = '';
      this.searchItem();
    },
    methods: {
      async searchItem() {
        this.tableData = await this.$httpN.postB('mpb/report/advert',{
          from: this.datePhase.value,
          to: this.datePhase.to,
          dateType: this.datePhase.dateType,
          platId: this.platId,
        })
      },
      exportExcel(){
        this.$nextTick(function () {
          let wb = XLSX.utils.table_to_book(this.$refs.table.$el);
          let wbout = XLSX.write(wb, { bookType: 'xlsx', bookSST: true, type: 'array' });
          let name = this.name ? this.name: '品牌推广总'
          try {
            FileSaver.saveAs(new Blob([wbout], { type: 'application/octet-stream' }), `${name}报表.xlsx`)
          } catch (e) {
            if (typeof console !== 'undefined') console.log(e, wbout)
          }
          return wbout;
        });
      }
    }
  }
</script>

<style lang="scss" scoped>

</style>
